package cn.hncu.booksManagementSystem.in.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;
import cn.hncu.booksManagementSystem.in.business.ebi.InEbi;
import cn.hncu.booksManagementSystem.in.business.factory.InEbiFactory;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import cn.hncu.booksManagementSystem.in.vo.InMainQueryModel;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.business.factory.UserEbiFactory;
import cn.hncu.booksManagementSystem.user.vo.UserModel;
import cn.hncu.booksManagementSystem.utils.DateUtil;

public class Querypanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTextField tfdInDetailId;
	private JTextField tfdSumNum;
	private JTextField tfdSumMoney;
	private JTextField tfdSumNum2;
	private JTextField tfdSumMoney2;
	
	private JFrame mainFrame;
	private JComboBox<String> cbbxBookId;
	private JTextField tfdInMainId;
	private JTextField tfdInDate2;
	private JTextField tfdInDate;
	//注入ebi
	UserEbi userEbi = UserEbiFactory.getInstanceOfUserEbi();
	BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	InEbi inEbi = InEbiFactory.getInEbi();
	
	private JComboBox<String> cbbxInUser;
	/**
	 * Create the panel.
	 */
	public Querypanel( JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
		initData();
	}
	private void initData() {
		Collection<UserModel> allInAndAdmin = userEbi.getAllInAndAdmin();
		cbbxInUser.addItem("查询所有");
		for (UserModel user : allInAndAdmin) {
			cbbxInUser.addItem(user.getId()+", "+user.getName());
		}
		Collection<BookModel> books = bookEbi.getAll();
		cbbxBookId.addItem("查询所有");
		for (BookModel book : books) {
			cbbxBookId.addItem(book.getId()+",《"+book.getName()+"》");
		}
	}
	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("进货单查询");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(275, 33, 242, 38);
		add(label);
		
		JLabel lblId = new JLabel("进货明细Id:");
		lblId.setHorizontalAlignment(SwingConstants.CENTER);
		lblId.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		lblId.setBounds(61, 265, 133, 38);
		add(lblId);
		
		JLabel lblid_1 = new JLabel("图书：");
		lblid_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblid_1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		lblid_1.setBounds(437, 265, 95, 38);
		add(lblid_1);
		
		tfdInDetailId = new JTextField();
		tfdInDetailId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdInDetailId.setColumns(10);
		tfdInDetailId.setBounds(204, 265, 199, 38);
		add(tfdInDetailId);
		
		tfdSumNum = new JTextField();
		tfdSumNum.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdSumNum.setColumns(10);
		tfdSumNum.setBounds(204, 329, 199, 38);
		add(tfdSumNum);
		
		JLabel label_1 = new JLabel("最少数量：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_1.setBounds(61, 329, 133, 38);
		add(label_1);
		
		tfdSumMoney = new JTextField();
		tfdSumMoney.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdSumMoney.setColumns(10);
		tfdSumMoney.setBounds(204, 395, 199, 38);
		add(tfdSumMoney);
		
		JLabel label_2 = new JLabel("最少金额：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_2.setBounds(63, 395, 133, 38);
		add(label_2);
		
		tfdSumNum2 = new JTextField();
		tfdSumNum2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdSumNum2.setColumns(10);
		tfdSumNum2.setBounds(542, 329, 199, 38);
		add(tfdSumNum2);
		
		tfdSumMoney2 = new JTextField();
		tfdSumMoney2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdSumMoney2.setColumns(10);
		tfdSumMoney2.setBounds(542, 395, 199, 38);
		add(tfdSumMoney2);
		
		JLabel label_3 = new JLabel("最多数量：");
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_3.setBounds(413, 329, 133, 38);
		add(label_3);
		
		JLabel label_4 = new JLabel("最多金额：");
		label_4.setHorizontalAlignment(SwingConstants.CENTER);
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_4.setBounds(413, 395, 133, 38);
		add(label_4);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setForeground(Color.BLUE);
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(474, 459, 113, 45);
		add(btnBack);
		
		JButton btnQuery = new JButton("查询");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnQueryActionPerformed(e);
			}
		});
		btnQuery.setForeground(Color.BLUE);
		btnQuery.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnQuery.setBounds(254, 459, 113, 45);
		add(btnQuery);
		
		cbbxBookId = new JComboBox<String>();
		cbbxBookId.setMaximumRowCount(4);
		cbbxBookId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		cbbxBookId.setBounds(542, 265, 187, 38);
		add(cbbxBookId);
		
		tfdInMainId = new JTextField();
		tfdInMainId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdInMainId.setColumns(10);
		tfdInMainId.setBounds(204, 120, 199, 38);
		add(tfdInMainId);
		
		JLabel lblid = new JLabel("进货单Id:");
		lblid.setHorizontalAlignment(SwingConstants.CENTER);
		lblid.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		lblid.setBounds(61, 120, 133, 38);
		add(lblid);
		
		cbbxInUser = new JComboBox<String>();
		cbbxInUser.setMaximumRowCount(4);
		cbbxInUser.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		cbbxInUser.setBounds(542, 120, 187, 38);
		add(cbbxInUser);
		
		JLabel label_6 = new JLabel("进货人：");
		label_6.setHorizontalAlignment(SwingConstants.CENTER);
		label_6.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_6.setBounds(422, 120, 124, 38);
		add(label_6);
		
		tfdInDate2 = new JTextField();
		tfdInDate2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdInDate2.setColumns(10);
		tfdInDate2.setBounds(542, 194, 199, 38);
		add(tfdInDate2);
		
		JLabel label_7 = new JLabel("截止时间：");
		label_7.setHorizontalAlignment(SwingConstants.CENTER);
		label_7.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_7.setBounds(413, 194, 133, 38);
		add(label_7);
		
		JLabel label_8 = new JLabel("起始时间：");
		label_8.setHorizontalAlignment(SwingConstants.CENTER);
		label_8.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_8.setBounds(61, 194, 133, 38);
		add(label_8);
		
		tfdInDate = new JTextField();
		tfdInDate.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdInDate.setColumns(10);
		tfdInDate.setBounds(204, 194, 199, 38);
		add(tfdInDate);
		
		JLabel lblNewLabel = new JLabel("日期格式：2018-04-23");
		lblNewLabel.setForeground(Color.RED);
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel.setBounds(204, 159, 199, 38);
		add(lblNewLabel);
	}
	protected void btnQueryActionPerformed(ActionEvent e) {
		//1收集参数(格式校验)inMainQueryModel和inDetailQueryModel
		//2组织参数
		//inMainQueryModel
		InMainQueryModel imqm = null;
		try {
			imqm = getInMainQueryModel();
		} catch (IllegalArgumentException e2) {
			JOptionPane.showMessageDialog(mainFrame, e2.getMessage());
			return;
		}
		//inDetailQueryModel
		InDetailQueryModel idqm = null;
		try {
			idqm = getInDetailQueryModel();
		} catch (IllegalArgumentException e1) {
			JOptionPane.showMessageDialog(mainFrame, e1.getMessage());
			return;
		}
		//补inId
		idqm.setInId(imqm.getId());
		//3调用逻辑层
		Collection<InMainModel> resCol = inEbi.getInMainsByCondition(imqm, idqm);
		//4导向结果界面
		mainFrame.setContentPane(new ListPanel(mainFrame,resCol,idqm));
		mainFrame.validate();
	}
	/**
	 * 收集(带格式校验)inId(缺),inDetailId,bookId,sumNum,sumNum2,sumMoney,sumMoney2
	 * 并封装成一个查询值对象InDetailQueryModel
	 * @return 查询值对象
	 */
	private InDetailQueryModel getInDetailQueryModel() throws IllegalArgumentException{
		//1收集 inDetailId,bookId,sumNum,sumNum2,sumMoney,sumMoney2
		//inDetailId
		String id = tfdInDetailId.getText().trim();
		if (!"".equals(id)) {
			if (!id.matches("^\\d{1,10}$")) {
				throw new IllegalArgumentException("进货明细Id,只能由0-9的数组组成，且长度在1-10之间");
			}
		}
		//bookId
		String strBook = cbbxBookId.getSelectedItem().toString().trim();
		String bookId = strBook.split(",")[0];
		if ("查询所有".equals(bookId)) {
			bookId = "";
		}
		//sumNum
		String strSumNum = tfdSumNum.getText().trim();
		int sumNum = 0;
		if (!"".equals(strSumNum)) {
			try {
				sumNum = Integer.parseInt(strSumNum);
			} catch (NumberFormatException e1) {
				throw new IllegalArgumentException("最少进货数量格式错误！");
			}
			if (sumNum<=0) {
				throw new IllegalArgumentException("最少进货数量不能小于等于0");
			}
		}
		//sumNum2
		String strSumNum2 = tfdSumNum2.getText().trim();
		int sumNum2 = 0;
		if (!"".equals(strSumNum2)) {
			try {
				sumNum2 = Integer.parseInt(strSumNum2);
			} catch (NumberFormatException e1) {
				throw new IllegalArgumentException("最多进货数量格式错误！");
			}
			if (sumNum2<=0) {
				throw new IllegalArgumentException("最多进货数量不能小于等于0");
			}
		}
		//sumMoney
		String strSumMoney = tfdSumMoney.getText().trim();
		double sumMoney= 0;
		if (!"".equals(strSumMoney)) {
			try {
				sumMoney = Double.parseDouble(strSumMoney);
			} catch (NumberFormatException e1) {
				throw new IllegalArgumentException("最少金额格式错误！");
			}
			if (sumMoney<=0) {
				throw new IllegalArgumentException("最少金额不能小于等于0");
			}
		}
		//sumMoney2
		String strSumMoney2 = tfdSumMoney2.getText().trim();
		double sumMoney2= 0;
		if (!"".equals(strSumMoney2)) {
			try {
				sumMoney2 = Double.parseDouble(strSumMoney2);
			} catch (NumberFormatException e1) {
				throw new IllegalArgumentException("最多金额格式错误！");
			}
			if (sumMoney2<=0) {
				throw new IllegalArgumentException("最多金额不能小于等于0");
			}
		}
		InDetailQueryModel idqm = new InDetailQueryModel();
		idqm.setId(id);
		idqm.setBookId(bookId);
		idqm.setSumNum(sumNum);
		idqm.setSumNum2(sumNum2);
		idqm.setSumMoney(sumMoney);
		idqm.setSumMoney2(sumMoney2);
		return idqm;
	}
	/**
	 * 收集(带格式校验) inMainId,inUserId,inDate,inDate2并封装成一个查询值对象
	 * @return 查询值对象
	 */
	private InMainQueryModel getInMainQueryModel()throws IllegalArgumentException {
		//1 收集参数 inMainId,inUserId,inDate,inDate2
		//inMainId
		String inMainId = tfdInMainId.getText().trim();
		if (inMainId!=null&&inMainId.length()>0) {
			if (!inMainId.matches("^\\d{1,10}$")) {
				throw new IllegalArgumentException("进货单Id只能由0-9的数组组成，且长度在1-10之间");
			}
		}
		
		//inUserId
		String strUser = cbbxInUser.getSelectedItem().toString().trim();
		String inUserId = strUser.split(",")[0];
		if ("查询所有".equals(inUserId)) {
			inUserId = "";
		}
		
		//inDate
		long inDate = 0;
		String strInDate = tfdInDate.getText().trim();
		if (strInDate!=null && strInDate.length()>0) {
			try {
				//时间小细节,起始时间应该是当天的00:00:00;
				strInDate = strInDate+" 00:00:00";
				inDate = DateUtil.string2Long(strInDate);
			} catch (ParseException e) {
				throw new IllegalArgumentException("起始时间格式错误");
			}
		}
		
		
		//inDate2
		long inDate2 = 0;
		String strInDate2 = tfdInDate2.getText().trim();
		if (strInDate2!=null && strInDate2.length()>0) {
			try {
				//时间小细节,起始时间应该是当天的23:59:59;
				strInDate2 = strInDate2+" 23:59:59";
				inDate2 = DateUtil.string2Long(strInDate2);
			} catch (ParseException e) {
				throw new IllegalArgumentException("截止时间格式错误");
			}
		}
		//组织参数
		InMainQueryModel imqm = new InMainQueryModel();
		imqm.setId(inMainId);
		imqm.setInUserId(inUserId);
		imqm.setInDate(inDate);
		imqm.setInDate2(inDate2);
		return imqm;
	}
}
